package com.unicair.hancher.disableapp.utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 作者：Hancher
 * 时间：2019/1/12.
 * 邮箱：ytu_shaoweijie@163.com
 * 版本：v1.0
 * <p>
 * 说明：
 */
public class HancherLogUtil {
    private static final String TAG = "HancherLogUtil";

    private final static boolean FORCE = true;

    private final static int SHOW_VERBOSE = 0x1;
    private final static int SHOW_INFO = 0x2;
    private final static int SHOW_DEBUG = 0x4;
    private final static int SHOW_WARNING = 0x8;
    private final static int SHOW_ERROR = 0x16;

    private final static int SHOW = SHOW_INFO | SHOW_ERROR;

    public static void v(String message) {
        if (FORCE || (SHOW & SHOW_VERBOSE) != 0)
            Log.v(TAG, formatMessage(message));
    }

    public static void i(String message) {
        if (FORCE || (SHOW & SHOW_INFO) != 0)
            Log.i(TAG, formatMessage(message));
    }

    public static void d(String message) {
        if (FORCE || (SHOW & SHOW_DEBUG) != 0)
            Log.d(TAG, formatMessage(message));
    }

    public static void w(String message) {
        if (FORCE || (SHOW & SHOW_WARNING) != 0)
            Log.w(TAG, formatMessage(message));
    }

    public static void e(String message) {
        if (FORCE || (SHOW & SHOW_ERROR) != 0)
            Log.e(TAG, formatMessage(message));
    }

    private static String formatMessage(String message) {
        /**
         * i=3 HancherLogUtil.v()
         * i=4 who call it
         */
        int i = 4;
        StackTraceElement[] targetElement = Thread.currentThread().getStackTrace();

        String fileName = targetElement[i].getFileName();   //文件名
        String className = targetElement[i].getClassName(); //类名
        int lineNum = targetElement[i].getLineNumber();     //行数
        String methName = targetElement[i].getMethodName(); //方法名

        //严格按（FileName:LineNuber）的格式来写 才可以定位，后面不能加东西的样子
        String str = fileName.substring(0, fileName.length() - 4) + methName + "(): " +
                message +
                " (" + fileName + ":" + lineNum + ")";
        return str;
    }

    public static void longToast(Context context, String message) {
        if (FORCE || (SHOW & SHOW_INFO) != 0)
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    public static void shortToast(Context context, String message) {
        if (FORCE || (SHOW & SHOW_INFO) != 0)
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static void longToastDebug(Context context, String message) {
        if (FORCE || (SHOW & SHOW_DEBUG) != 0)
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    public static void shortToastDebug(Context context, String message) {
        if (FORCE || (SHOW & SHOW_DEBUG) != 0)
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static StringBuilder o(Object o) {
        StringBuilder log = new StringBuilder();
        String[] fieldNames = getFiledName(o);
        for (int j = 0; j < fieldNames.length; j++) {     //遍历所有属性
            String name = fieldNames[j];    //获取属性的名字
            Object value = getFieldValueByName(name, o);
            if (value == null) continue;
            log.append(name);
            log.append("=");
            log.append(value);
            if (j != fieldNames.length - 1)
                log.append(", ");
        }
        return log;
    }

    /**
     * 获取属性名数组
     */
    private static String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
//            System.out.println(fields[i].getType());
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * 根据属性名获取属性值
     */
    private static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(o, new Object[]{});
            return value;
        } catch (Exception e) {
            return null;
        }
    }


}
